package org.ztest.async;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;

public class ReadFileAsync
{
	public void readFile(String file, Consumer<String> success, Consumer<Throwable> fail)
	{
		try
		{
			success.accept(new String(Files.readAllBytes(Paths.get(file))));
		}
		catch (Exception e)
		{
			fail.accept(e);
		}
	}
	
	public CompletableFuture<String> readFileAsync(String file)
	{
		return CompletableFuture.supplyAsync(() -> {try {
			return new String(Files.readAllBytes(Paths.get(file)));
			}
		catch (Exception e)
		{
			throw new RuntimeException(e);
		}
		});
	}
}
